package org.budo.mybatis.dao.statement.builder.creator;

import java.util.List;

import org.apache.ibatis.session.Configuration;
import org.budo.mybatis.dao.statement.builder.impl.DefaultStatementBuilder;
import org.budo.support.lang.util.StringUtil;

/**
 * @author lmw
 */
public class UpdateStatementCreator extends AbstractUpdateStatementCreator {
    public UpdateStatementCreator(Configuration configuration, DefaultStatementBuilder statementBuilder) {
        super(configuration, statementBuilder);
    }

    protected String getStatementId() {
        return STATEMENT_UPDATE;
    }

    protected String createStatement(Class<?> entityType) {
        String tableName = this.getStatementBuilder().getEntityConfiger().getTableName(entityType);
        String idColumn = this.getStatementBuilder().getEntityConfiger().getIdColumn(entityType);
        String idFieldName = this.getStatementBuilder().getEntityConfiger().getIdFieldName(entityType);

        List<String> updateSetSqls = this.updateSetSqls(this.getStatementBuilder().getEntityConfiger(), entityType, false);

        String statementXml = _4 + "<update id=\"" + this.getStatementId() + "\" parameterType=\"" + entityType.getName() + "\">\n" //
                + _8 + "UPDATE " + tableName + " SET\n" //
                + _12 + StringUtil.join(updateSetSqls, ",\n" + _12) + "\n" //
                + _8 + " WHERE " + idColumn + "=#{" + idFieldName + "}" + "\n" //
                + _4 + "</update>"; //

        return statementXml;
    }
}